package com.zkl.common.web.app.base;

/*import com.zkl.common.core.constants.GlobalConstant;
import com.zkl.common.core.constants.user.UserConstant;
import com.zkl.common.core.entity.dto.merchant.BrandDevConfigDto;
import com.zkl.common.core.entity.dto.user.UserInfoDto;
import com.zkl.common.core.entity.vo.ResultVo;
import com.zkl.common.core.exception.BaseException;
import com.zkl.common.core.exception.SystemErrorType;
import com.zkl.common.core.exception.UserServiceErrorType;
import com.zkl.common.core.exception.UserServiceException;
import com.zkl.common.web.app.rpc.merchant.MerchantFeignService;
import com.zkl.common.web.app.utils.StringUtils;
import com.zkl.common.web.redis.CacheHashCode;
import com.zkl.common.web.redis.CacheUtils;*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 基础控制器
 */
@Controller
public class BaseController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

   /* @Autowired
    private CacheUtils cacheUtils;

    @Autowired
    private MerchantFeignService merchantFeignService;

    *//**
     * 获取品牌商配置信息
     * @return
     *//*
    public BrandDevConfigDto getBrandInfo() {
        String corpCode = getRequest().getHeader(UserConstant.CORP_CODE);;
        if (StringUtils.isEmpty(corpCode)) {
            String appId = getAppId();
            int hashCode = CacheHashCode.of(appId).hashCode();
            BrandDevConfigDto brandDevConfigDto = cacheUtils.get("BrandDevConfig:" + hashCode, BrandDevConfigDto.class);
            if (brandDevConfigDto == null) {
                ResultVo<BrandDevConfigDto> result = merchantFeignService.getByAppId(appId);
                brandDevConfigDto = result.getData();
                if (brandDevConfigDto == null) {
                    throw BaseException.businessException(SystemErrorType.ARGUMENT_NOT_VALID);
                }
            }
            return brandDevConfigDto;
        } else {
            int hashCode = CacheHashCode.of(corpCode).hashCode();
            BrandDevConfigDto brandDevConfigDto = cacheUtils.get("BrandDevConfig:" + hashCode, BrandDevConfigDto.class);
            if (brandDevConfigDto == null) {
                ResultVo<BrandDevConfigDto> result = merchantFeignService.getByCorpCode(corpCode);
                brandDevConfigDto = result.getData();
            }
            return brandDevConfigDto;
        }
    }

    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

	public String getAppId() {
		String appId = getRequest().getHeader(GlobalConstant.APP_ID);
		if(StringUtils.isEmpty(appId)) {
			throw BaseException.businessException(SystemErrorType.ARGUMENT_NOT_VALID);
		}
		return appId;
	}

	public UserInfoDto getUserInfoDto() {
        String userToken = getUserToken();
        String recommendCode = getRecommendUserCode();
        UserInfoDto userInfoDto = cacheUtils.get(UserConstant.user_info + recommendCode + userToken, UserInfoDto.class);
        if (userInfoDto == null) {
            throw UserServiceException.businessException(UserServiceErrorType.USER_TOKEN_ERROR);
        }
        return userInfoDto;
    }

	public String getUserToken() {
		String userToken = getRequest().getHeader(GlobalConstant.USER_TOKEN);
		if(StringUtils.isEmpty(userToken)) {
            throw BaseException.businessException(SystemErrorType.ARGUMENT_NOT_VALID);
		}
		return userToken;
	}

    public String getRecommendCode() {
        String recommendCode = getRequest().getHeader(UserConstant.RECOMMEND_CODE);
        if(StringUtils.isEmpty(recommendCode)) {
            throw BaseException.businessException(SystemErrorType.ARGUMENT_NOT_VALID);
        }
        return recommendCode;
    }

    public String getRecommendUserCode() {
        String recommendUserCode = getRequest().getHeader(UserConstant.RECOMMEND_USER_CODE);
        if(StringUtils.isEmpty(recommendUserCode)) {
            throw BaseException.businessException(SystemErrorType.ARGUMENT_NOT_VALID);
        }
        return recommendUserCode;
    }

    public String getCorpCode() {
        String corpCode = getRequest().getHeader(UserConstant.CORP_CODE);
        if(StringUtils.isEmpty(corpCode)) {
            throw BaseException.businessException(SystemErrorType.ARGUMENT_NOT_VALID);
        }
        return corpCode;
    }

    *//**
     * 获取用户真实IP地址，不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
     *
     * 可是，如果通过了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP值，究竟哪个才是真正的用户端的真实IP呢？
     * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
     *
     * 如：X-Forwarded-For：192.168.1.110, 192.168.1.120, 192.168.1.130,
     * 192.168.1.100
     *
     * 用户真实IP为： 192.168.1.110
     *
     * @param request
     * @return
     *//*
    protected String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if(ip.equals("127.0.0.1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ip= inet.getHostAddress();
            }
            //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if(ip!=null && ip.length()>15){ //"***.***.***.***".length() = 15
                if(ip.indexOf(",")>0){
                    ip = ip.substring(0,ip.indexOf(","));
                }
            }
        }
        return ip;
    }

    protected String readData(HttpServletRequest request) {
        BufferedReader br = null;
        try {
            br = request.getReader();
            String line = br.readLine();
            String var4;
            if (line == null) {
                var4 = "";
                return var4;
            } else {
                StringBuilder ret = new StringBuilder();
                ret.append(line);

                while((line = br.readLine()) != null) {
                    ret.append('\n').append(line);
                }

                var4 = ret.toString();
                return var4;
            }
        } catch (IOException var14) {
            throw new RuntimeException(var14);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException var13) {
                    logger.error(var13.getMessage(), var13);
                }
            }

        }
    }*/
}
